package jp.sourceforge.akjrcp.ffmpeg;

import java.io.File;

import org.eclipse.ui.PlatformUI;

/**
 * 
 * @author aki
 * This class handle only pure_ffmpeg.exe
 * and get value from preferences.
 *
 */
public class FFMpegUI {

	
	public static final String FFMPEG_PATH="ffmpeg.ffmpeg_path"; //$NON-NLS-1$
	public static final String PURE_FFMPEG_PATH="ffmpeg.pure_ffmpeg_path"; //$NON-NLS-1$
	public static final String USE_PURE_FFMPEG="ffmpeg.use_pure_ffmpeg"; //$NON-NLS-1$
	public static final String USE_THREAD="ffmpeg.use_thread"; //$NON-NLS-1$
	public static final String DEMUX_SOME_VIDEO="ffmpeg.demux_dome_video";
	public static final String ALERT_PTHREAD="ffmpeg.alert_pthread";

	public static boolean isPureFFMpegSupportedVideoCodec(String codec){
		String supportec[]={FFMpegOptions.CODEC_VIDEO_HUFFYUV,FFMpegOptions.CODEC_VIDEO_MJPEG,FFMpegOptions.CODEC_VIDEO_MPEG1,FFMpegOptions.CODEC_VIDEO_MSMPEG4V1,FFMpegOptions.CODEC_VIDEO_MSMPEG4V2,FFMpegOptions.CODEC_VIDEO_WMV1,FFMpegOptions.CODEC_VIDEO_WMV2,FFMpegOptions.CODEC_VIDEO_MSMPEG4,FFMpegOptions.CODEC_COPY};
	for (int i = 0; i < supportec.length; i++) {
		if(codec.equals(supportec[i])){
			return true;
		}
	}
			
			return false;
	}
	
	public static boolean isPureFFMpegSupportedAudioCodec(String codec){
		String supportec[]={FFMpegOptions.CODEC_COPY,FFMpegOptions.CODEC_AUDIO_MP2,FFMpegOptions.CODEC_AUDIO_WAV,FFMpegOptions.CODEC_AUDIO_WMA1,FFMpegOptions.CODEC_AUDIO_WMA2};
		
		for (int i = 0; i < supportec.length; i++) {
		if(codec.equals(supportec[i])){
			return true;
		}
	}
			
			return false;
	}
	/**
	 * 
	 * @return ffmpeg path or null if not setted
	 */
	public  static String getFFmpeg(){
		
		String ffmpeg=PlatformUI.getPreferenceStore()
		.getString(FFMpegUI.FFMPEG_PATH);
		
		if(ffmpeg.length()==0){
			return null;
		}else{
			return ffmpeg;
		}
	}
	/**
	 * 
	 * @return pure ffmpeg path or null if not setted
	 */
	public  static String getPureFFmpeg(){
		
		String ffmpeg=PlatformUI.getPreferenceStore()
		.getString(FFMpegUI.PURE_FFMPEG_PATH);
		
		if(ffmpeg.length()==0){
			return null;
		}else{
			return ffmpeg;
		}
	}
		/**
		 * 
		 * @return null if ffmpeg or pure_ffmpeg not found
		 */
	public  static String getFFmpegOrPureFFMpeg(){
		String ret=null;
		String ffmpeg=PlatformUI.getPreferenceStore()
		.getString(FFMpegUI.FFMPEG_PATH);
		String pure_ffmpeg=PlatformUI.getPreferenceStore()
		.getString(FFMpegUI.PURE_FFMPEG_PATH);
		
		if(ffmpeg.length()==0){
			ffmpeg= null;
		}
		
		if(pure_ffmpeg.length()==0){
			pure_ffmpeg= null;
		}
		
		
		if(pure_ffmpeg==null && ffmpeg!=null){
			ret=ffmpeg;
		}
		
		if(ffmpeg==null && pure_ffmpeg!=null){
			ret=pure_ffmpeg;
		}
		
		if(ffmpeg!=null && pure_ffmpeg!=null){
			if(PlatformUI.getPreferenceStore().getBoolean(FFMpegUI.USE_PURE_FFMPEG)){
				ret=pure_ffmpeg;
			}else{
				ret=ffmpeg;
			}
		}
		
		return ret;
	}
	
	public static boolean isUseThread(){
		return PlatformUI.getPreferenceStore().getBoolean(FFMpegUI.USE_THREAD);
	}
	
	public static boolean isAlertPThread(){
		return PlatformUI.getPreferenceStore().getBoolean(FFMpegUI.ALERT_PTHREAD);
	}
	
	public static boolean isUsePureFFMpeg(){
		return PlatformUI.getPreferenceStore().getBoolean(FFMpegUI.USE_PURE_FFMPEG);
	}

		public static boolean isFFMpegOrPureFFMpegAvailable(String path){
			if(path!=null && !path.equals("")){
				if((path.endsWith("ffmpeg.exe")||path.endsWith("pure_ffmpeg.exe") )&& new File(path).exists()){
				return true;
				}
			}
			return false;
		}

		/*
		 * maybe some conflict happen
		 */
		public static boolean isPureFFMpeg(String path){
			return path.toLowerCase().endsWith("pure_ffmpeg.exe");	
//			this is wrong.because some code check here.
		//	MP3ConvertTabBuilder check if pure_ffmpeg.exe is setted show error
			//return path.toLowerCase().endsWith("pure_ffmpeg.exe") || path.toLowerCase().endsWith("ffmpeg.exe"); 
		}

}
